## Load required libraries
library(haven) 
library(ggplot2)  
library(dplyr) 
library(lubridate)  
library(readstata13)
library(ggrepel) 

setwd("/Users/alicezxu/Dropbox/Trump Polls/CPS Replication/")
data <- read.dta13("state_businessmandates.dta")

head(data)

## Convert the 'closed_business' variable to a date format if needed
data <- data %>%
  mutate(closed_business = as.Date(closed_business, format = "%Y-%m-%d"))

##############################################################################
## Figure 3: Staggered Timing of Business Closure Mandates by State
##############################################################################

## Create a color variable based on highcolgrad and lowcolgrad
data <- data %>%
  mutate(color = case_when(
    highcolgrad == 1 ~ "HighColGrad",
    lowcolgrad == 1 ~ "LowColGrad",
    TRUE ~ "Other"
  ))

setwd("/Users/alicezxu/Dropbox/Trump Polls/CPS Replication/")
pdf("State_policytiming_plot.pdf", width = 14, height = 10)
ggplot(data, aes(x = closed_business, y = state_code)) +
  geom_point(aes(color = color), size = 2, alpha = 0.7, position = position_jitter(width = 0.3, height = 0.2)) +  # Add jitter and color by category
  geom_text_repel(aes(label = state_code), size = 3, nudge_y = 0.3, segment.color = "grey50", segment.size = 0.2) +  # Repel text labels to avoid overlap
  labs(title = "Staggered Timing of Business Closure Mandates by State",
       x = "Date of Business Closure Mandate",
       y = "") + 
  scale_x_date(date_labels = "%b %d", date_breaks = "1 day") +  # Set x-axis to break daily
  scale_color_manual(
    values = c("HighColGrad" = "blue1", "LowColGrad" = "red3"),
    name = "Legend"  
  ) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        axis.text.y = element_blank(),  
        axis.ticks.y = element_blank(),  
        plot.margin = margin(20, 10, 50, 10, "pt"),
        legend.position = "bottom") +  
  coord_cartesian(clip = "off")  
dev.off()

##############################################################################
## Figure 4: Cumulative Number of States by High vs. Low Risk
##############################################################################

## Aggregate the data by count for 'highcolgrad' and 'lowcolgrad' over time
agg_data <- data %>%
  group_by(closed_business, category = ifelse(highcolgrad == 1, "Low Risk (More Educated)", "High Risk (Less Educated)")) %>%
  summarise(count = n()) %>%
  arrange(closed_business)

## Create the cumulative sum for each category
agg_data <- agg_data %>%
  group_by(category) %>%
  mutate(cumulative_count = cumsum(count))

## Plot it
ggplot(agg_data, aes(x = closed_business, y = cumulative_count, color = category, group = category)) +
  geom_line(size = 1.5) +
  geom_point(size = 6, shape = 21, fill = "white") +  
  geom_text(aes(label = cumulative_count), size = 3, color = "black", nudge_y = 0) +  
  scale_color_manual(values = c("Low Risk (More Educated)" = scales::alpha("red", 0.6),
                                "High Risk (Less Educated)" = scales::alpha("blue", 0.6))) +
  labs(title = "Cumulative Number of States by High and Low Risk",
       x = "Date of Business Closure Mandate",
       y = "Cumulative Number of States",
       color = "Category") +
  scale_x_date(date_labels = "%b %d", date_breaks = "1 day") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        plot.margin = margin(20, 10, 50, 10, "pt"),
        legend.position = "bottom",
        legend.key = element_blank(),  
        legend.key.size = unit(1.5, "lines"),
        legend.title = element_blank()) + 
  guides(color = guide_legend(override.aes = list(size = 1.5, linetype = 1))) +  
  coord_cartesian(clip = "off")

